import { request } from 'umi';
import { formatData } from '@/utils/util';
import type { IUser, IToken } from '@/types/user';
import qs from 'qs';

/** 登录 */
export async function apiLogin(params: Omit<IUser, 'isRemember'>) {
  return request('login', {
    method: 'POST',
    body: formatData<IUser>(params),
  });
}

/** 获取用户 */
export async function getUserByToken(params: IToken) {
  return request(`loginByToken?${qs.stringify(params)}`, {
    method: 'POST',
  });
}

/** 初始化验证码code */
export async function initCode() {
  return request('/init', {
    method: 'GET',
  });
}

/** 获得验证码图片 */
export async function getCodeImg(captchaId: string) {
  return request(`/draw/${captchaId}`, {
    responseType: 'blob',
    method: 'GET',
  });
}
